package com.situ.studentmanagement.controller;

/**
 * Created by baba on 2016/8/2.
 */

import com.situ.studentmanagement.dao.AfficheMapper;
import com.situ.studentmanagement.dao.GoodsMapper;
import com.situ.studentmanagement.dao.OrderMapper;
import com.situ.studentmanagement.model.Affiche;
import com.situ.studentmanagement.model.Goods;
import com.situ.studentmanagement.model.Orderone;
import com.situ.studentmanagement.model.Ordertwo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static com.situ.studentmanagement.Constants.ORDER_NUMBER;
import static com.situ.studentmanagement.Constants.USER_ID;
import static com.situ.studentmanagement.Constants.USER_NAME;

@Controller //表示这个类是一个Controller，于是当请求路径 / 时，这个Contrller的实例被自动创建
@RequestMapping("/goods") //这个controller对应到路径 /
public class goods {
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    AfficheMapper afficheMapper;
    @Autowired
    OrderMapper orderMapper;
    @RequestMapping("/selectOne")
    public String register(Model model, HttpSession session,
                           @RequestParam("goods_id")String goods_id){
        return "";
    }
    @RequestMapping("/selectHot")
    public String selectHot(Model model, HttpSession session,
                            @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index ){
        if(session.getAttribute(USER_ID)!=null){
            model.addAttribute("name",session.getAttribute(USER_NAME));
            model.addAttribute("message",1);
        }
        int rowsOfPage=9;
        List<Goods> goodses=goodsMapper.getHotGoods(page_start_index,rowsOfPage);
        List<Goods> goodAll=goodsMapper.selectAllGood();
        int goodsLength=goodAll.size();
        model.addAttribute("goodsLength",goodsLength);
        model.addAttribute("goods",goodses);
        List<Affiche> affiches=afficheMapper.selectAllAffiche();
        model.addAttribute("affiches",affiches);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        return "hotgoods";
    }
    @RequestMapping("/selectFree")
    public String selectFree(Model model, HttpSession session,
                            @RequestParam(value="page_start_index",defaultValue = "0") int page_start_index ){
        if(session.getAttribute(USER_ID)!=null){
            model.addAttribute("name",session.getAttribute(USER_NAME));
            model.addAttribute("message",1);
        }
        int rowsOfPage=9;
        List<Goods> goodses=goodsMapper.getFreeGoods(page_start_index,rowsOfPage);
        List<Goods> goodAll=goodsMapper.selectAllGood();
        int goodsLength=goodAll.size();
        model.addAttribute("goodsLength",goodsLength);
        model.addAttribute("goods",goodses);
        List<Affiche> affiches=afficheMapper.selectAllAffiche();
        model.addAttribute("affiches",affiches);
        if(page_start_index>0) {
            int start_index=page_start_index-rowsOfPage;
            if(start_index<0){
                start_index=0;
            }
            model.addAttribute("page_pre_index", start_index );
        }

        model.addAttribute("page_next_index",page_start_index+rowsOfPage);
        return "freegoods";
    }

//购物车
    @RequestMapping("/gotocar")
    public String gotocar(Model model, HttpSession session){
        int finalprice=0;
        if(session.getAttribute(USER_ID)!=null){
            model.addAttribute("name",session.getAttribute(USER_NAME));
            model.addAttribute("message",1);
        }else {
            List<Affiche> affiches=afficheMapper.selectAllAffiche();
            model.addAttribute("affiches",affiches);
            //查询说有的菜品
            List<Goods> goodses=goodsMapper.selectAllGoods();
            model.addAttribute("goods",goodses);
            model.addAttribute("iskong",0);
            return "index";
        }
        List<Affiche> affiches=afficheMapper.selectAllAffiche();
        model.addAttribute("affiches",affiches);

        //查询是否购物
        Long ordernumber= (Long) session.getAttribute(ORDER_NUMBER);
        int userid= (int)session.getAttribute(USER_ID);
        List<Ordertwo> ordertwos=orderMapper.selectOneOrder(ordernumber);
        if(ordertwos.size()==0){
            model.addAttribute("noshopping",0);
            return "car";
        }
        for(int i=0;i<ordertwos.size();i++){
            finalprice=finalprice+ordertwos.get(i).getPrice();
        }
        model.addAttribute("ordertwos",ordertwos);
        model.addAttribute("finalprice",finalprice);
        return "car";
    }

    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    public String modify(Model model, HttpSession session, ServletRequest request){
        if(session.getAttribute(USER_ID)!=null){
            model.addAttribute("name",session.getAttribute(USER_NAME));
            model.addAttribute("message",1);
        }
        List<Affiche> affiches=afficheMapper.selectAllAffiche();
        model.addAttribute("affiches",affiches);
        int finalPrice=0;
        Long ordernumber= (Long) session.getAttribute(ORDER_NUMBER);
        List<Ordertwo> ordertwos=orderMapper.selectOneOrder(ordernumber);
        //遍历订单详表并计算出总的金额是多少
        for(int i=0;i<ordertwos.size();i++){
            int goodid=ordertwos.get(i).getGoodid();
            String number=request.getParameter(ordertwos.get(i).getGoodid().toString());
            int goodNumber=Integer.parseInt(number);
            orderMapper.setGoodNumber(ordernumber,goodid,goodNumber);
            int thisPrice=goodNumber*ordertwos.get(i).getPrice();
            finalPrice=finalPrice+thisPrice;
        }
        //修改订单总表中的金额
        orderMapper.setFinalPrice(ordernumber,finalPrice);
        //查询订单总表
        Orderone orderone=orderMapper.selectThisOrder(ordernumber);
        model.addAttribute("orderone",orderone);
        return "pay";
    }
    @RequestMapping(value = "/success",method = RequestMethod.POST)
    public String success(Model model, HttpSession session,
                          @RequestParam("paytype")String paytype){
        if(session.getAttribute(USER_ID)!=null){
            model.addAttribute("name",session.getAttribute(USER_NAME));
            model.addAttribute("message",1);
        }
        List<Affiche> affiches=afficheMapper.selectAllAffiche();
        model.addAttribute("affiches",affiches);

        Long ordernumber= (Long) session.getAttribute(ORDER_NUMBER);
        String userpaytype=null;
        int protectint=Integer.parseInt(paytype);
        switch (protectint){
            case 1:
                userpaytype="货到付款";
                break;
            case 2:
                userpaytype="网银";
                break;
            case 3:
                userpaytype="支付宝";
                break;
            case 4:
                userpaytype="先赊账";
                break;
        }
        Date date=new Date();
        DateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time=format.format(date);
        //设置时间
        orderMapper.setTime(ordernumber,time);
        int userid= (int)session.getAttribute(USER_ID);
        //设置已付款
//        orderMapper.setIsPay(ordernumber);
        //设置付款方式
        orderMapper.setPaytype(ordernumber,userpaytype);
        List<Orderone> orderones=orderMapper.seleAllOrderoneByUserId(userid);
        model.addAttribute("orderone",orderones);
        return "selectorder";
    }
//查看订单情况
    @RequestMapping("/gotoorder")
    public String gotoorder(Model model, HttpSession session){

        if(session.getAttribute(USER_ID)!=null){
            model.addAttribute("name",session.getAttribute(USER_NAME));
            model.addAttribute("message",1);
        }else {
            List<Affiche> affiches=afficheMapper.selectAllAffiche();
            model.addAttribute("affiches",affiches);
            //查询说有的菜品
            List<Goods> goodses=goodsMapper.selectAllGoods();
            model.addAttribute("goods",goodses);
            model.addAttribute("iskong",0);
            return "index";
        }
        List<Affiche> affiches=afficheMapper.selectAllAffiche();
        model.addAttribute("affiches",affiches);
        int userid= (int)session.getAttribute(USER_ID);
        List<Orderone> orderones=orderMapper.seleAllOrderoneByUserId(userid);
        model.addAttribute("orderone",orderones);
        return "selectorder";
    }
    //查看某一条等单的内容
    @RequestMapping("/selectcar")
    public String selectcar(Model model, HttpSession session,
                            @RequestParam("orderid")long orderid){

    List<Ordertwo> ordertwos =orderMapper.selectOneOrder(orderid);
        model.addAttribute("ordertwos",ordertwos);
        return "checkoneorder";
    }
    @RequestMapping("/selectcar2")
    public String selectcar2(Model model, HttpSession session,
                            @RequestParam("orderid")long orderid){

        List<Ordertwo> ordertwos =orderMapper.selectOneOrder(orderid);
        model.addAttribute("ordertwos",ordertwos);
        return "manageselectone";
    }

    @RequestMapping("/setispost")
    public String setispost(Model model, HttpSession session,
                             @RequestParam("orderid")long orderid){

        orderMapper.setispostByOrderid(orderid);
        List<Orderone> orderones=orderMapper.selectAllOrderOne();
        model.addAttribute("orderones",orderones);
        return "management";
    }

    @RequestMapping("/confirm")
    public String confirm(Model model, HttpSession session,
                            @RequestParam("orderid")long orderid){
        orderMapper.confirmorder(orderid);
        int userid= (int)session.getAttribute(USER_ID);
        List<Orderone> orderones=orderMapper.seleAllOrderoneByUserId(userid);
        model.addAttribute("orderone",orderones);
        model.addAttribute("message",1);
        model.addAttribute("name",session.getAttribute(USER_NAME));
        return "selectorder";
    }





}
